// AWTTextArea.cpp: implementation of the AWTTextArea class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "awt.h"
#include "AWTTextArea.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

IMPLEMENT_DYNAMIC(AWTTextArea, AWTTextComponent)

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

//AWTTextArea::AWTTextArea()
//{

//}

AWTTextArea::~AWTTextArea()
{

}

CWnd* AWTTextArea::createComponent()					 
{
	CWnd* wParent = getParent()->getPeer();
	if (wParent == NULL)
		return wParent;
	AWTTextAreaPeer* pwnd = new AWTTextAreaPeer(this);
	DWORD dwStyle = WS_CHILD | WS_VISIBLE | ES_AUTOVSCROLL | ES_MULTILINE;
	pwnd->Create(dwStyle, getBounds(), wParent, 0);
	pwnd->ModifyStyleEx(0, WS_EX_CLIENTEDGE);
	pwnd->SetFont(CFont::FromHandle(m_font));
	pwnd->SetMargins(1,1);
	pwnd->setEditable(m_editable);
	if (!m_enabled)
		pwnd->EnableWindow(FALSE);
	pwnd->setText(m_text);
	return pwnd;
}

CSize AWTTextArea::minimumSize(int rows, int columns) 
{
	return preferredSize(rows, columns);
}

CSize AWTTextArea::minimumSize() 
{
//	return CSize(10, 10);
	return preferredSize(m_rows, m_columns);
}

CSize AWTTextArea::preferredSize(int rows, int columns) 
{
	HDC hdc = ::GetDC(NULL);
	CDC* pdc = CDC::FromHandle(hdc);
	CFont* ft = CFont::FromHandle(getFont());
	CFont* ftOld = pdc->SelectObject(ft);
	TEXTMETRIC tm;
	pdc->GetTextMetrics(&tm);

	CSize sz = CSize(10,10);

	if (columns > 0)
	{
		TCHAR* c = (TCHAR*)malloc((columns+1) * sizeof(TCHAR));
		for (int n = 0; n < columns; n++)
			c[n] = 'X';
		c[columns] = '\0';
		GetTextExtentPoint32(pdc->m_hDC, c, columns, &sz);
		sz.cx += 6;
		free(c);
	}
	pdc->SelectObject(ftOld);
	::ReleaseDC(NULL, hdc);	

	if (rows > 0)
	{
		sz.cy = (rows * (tm.tmHeight + tm.tmExternalLeading)) + 6;;
	}

	return sz;
}

CSize AWTTextArea::preferredSize() 
{
	return preferredSize(m_rows, m_columns);
}

